﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class dingdan_addqian : System.Web.UI.Page
{
// 2 请求参数成员变量列表
﻿    public string request_bianhao = "";
﻿    public string request_jine = "";
﻿    public string request_zhifushenhe = "";
﻿    public string request_fahuoshenhe = "";
﻿    public string request_shouhuoshenhe = "";
﻿    public string request_zhifufangshiid = "";
﻿    public string request_xingming = "";
﻿    public string request_dianhua = "";
﻿    public string request_dizhi = "";
﻿    public string request_usersid = "";
﻿    public string request_shijian = "";
﻿    public string request_wuliu = "";
﻿    public string request_beizhu = "";


    //初始化当前时间
    public string current_shijian = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss").Trim();
    //初始化当前日期
    public string current__riqi = DateTime.Now.ToString("yyyy-MM-dd").Trim();

    //默认执行页面载入代码
    protected void Page_Load(object sender, EventArgs e)
    {
//首先登陆判断
﻿
        //登录判断
        if (Session["usersidx"] != null && Session["usersidx"].ToString() != "")
        {
            //登录状态不为空
        }
        else
        {
            //登录状态为空
            Response.Write("<script>javascript:alert('对不起,您没有这个权限或未登录!');history.back();</script>");
            Response.End();
        }


        if (!IsPostBack)
        {
//3 初始化参数列表
﻿            if (IsExistControl("zhifufangshiid"))
            {
                addselect("zhifufangshi", "id", "mingcheng", "zhifufangshiid");
            }
﻿
            if (Session["usersidx"] != null && Session["usersidx"].ToString() != "")
            {
                string fatherIdColumnValue = "";
                fatherIdColumnValue = Session["usersidx"].ToString();
                if (IsExistControl("usersid"))
                {
                    addselectbyId("users", "id", "loginname", "usersid", fatherIdColumnValue);
                }
            }
            else
            {
                Response.Write("<script>javascript:alert('对不起,您没有这个权限或未登录!');history.back();</script>");
                Response.End();
            }
﻿            if (IsExistControl("shijian"))
            {
                GetControl("shijian").Text = current_shijian;
            }


            //订单编号与金额初始化
             Random rnd = new Random();
             int n = rnd.Next(1000, 9999);
             string newname = DateTime.Now.Year.ToString().Trim() + DateTime.Now.Month.ToString().Trim() + DateTime.Now.Day.ToString().Trim() + DateTime.Now.Hour.ToString().Trim() + DateTime.Now.Minute.ToString().Trim() + DateTime.Now.Second.ToString().Trim() + n.ToString().Trim();
﻿            if (IsExistControl("bianhao"))
            {
                GetControl("bianhao").Text = newname;
                GetTextBoxControl("bianhao").ReadOnly = true;
            }
            if (IsExistControl("jine"))
            {
                if (Request.QueryString["jine"].ToString() == "" || Request.QueryString["jine"].ToString().Equals("0"))
                {

                    Response.Write("<script>javascript:alert('对不起,请先添加购物车!');history.back();</script>");
                    Response.End();
                }
                GetTextBoxControl("jine").Text = Request.QueryString["jine"].ToString().Trim();
                GetTextBoxControl("jine").ReadOnly = true;
            }
            ////Response.Write("第一次,非回发");
        }
        else
        {
            ////Response.Write("第一次以后,回发");
        }
    }

    protected void Add_Button_Click(object sender, EventArgs e)
    {

﻿        //重复判断
        //if (IsExistControl("bianhao"))
        //{
            //ischongfu("select id from dingdan where  bianhao='" + bianhao.Text.ToString().Trim() + "'");
        //}


        //a库存判断,从购物车获取购买信息，从商品表获取库存信息并判断大小
        string usersid = "";
        //登录判断
        if (Session["usersidx"] != null && Session["usersidx"].ToString() != "")
        {
            //登录状态不为空
            usersid = Session["usersidx"].ToString();
        }
        else
        {
            //登录状态为空
            Response.Write("<script>javascript:alert('对不起,您没有这个权限或未登录!');history.back();</script>");
            Response.End();
        }
        string sqlgouwuche="select * from gouwuche where 1=1 and usersid='" + usersid + "'";
        DataSet dsresultgouwuche = new DataSet();
        dsresultgouwuche = new Class1().hsggetdata(sqlgouwuche);
        if (dsresultgouwuche != null)
        {

            if (dsresultgouwuche.Tables[0].Rows.Count > 0)
            {

                for (int i = 0; i < dsresultgouwuche.Tables[0].Rows.Count; i++)
                {
                    string shangpinid = dsresultgouwuche.Tables[0].Rows[i]["shangpinid"].ToString().Trim();
                    string goumaishuliang = dsresultgouwuche.Tables[0].Rows[i]["goumaishuliang"].ToString().Trim();

                    string kucunshuliang = getFatherSecondColumnValue("shangpin", "kucunshuliang", shangpinid);
                    if (int.Parse(kucunshuliang) < int.Parse(goumaishuliang))
                    {
                        Response.Write("<script>javascript:alert('库存不足，无法购买!!');history.back();</script>");
                        Response.End();
                    }
                }
            }
            else
            {
                Response.Write("<script>javascript:alert('对不起,请先添加购物车!');history.back();</script>");
                Response.End();
            }
        }
        else
        {
            Response.Write("<script>javascript:alert('对不起,请先添加购物车!');history.back();</script>");
            Response.End();
        }

        string sql = "";
        string sqlProperty = "";
        string sqlValue = "";
        string sqlTail = ")";

        sqlProperty = "insert into dingdan(";
        sqlValue = ") values(";


//4 组织sql语句的属性与值
﻿
            if (IsExistControl("bianhao"))
            {
                IsEmptyControl("bianhao");
    
                sqlProperty = sqlProperty + "bianhao,";
                sqlValue = sqlValue + "'" + GetControl("bianhao").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("jine"))
            {
                IsEmptyControl("jine");
    
                sqlProperty = sqlProperty + "jine,";
                sqlValue = sqlValue + "'" + GetControl("jine").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("zhifushenhe"))
            {
                IsEmptyControl("zhifushenhe");
    
                sqlProperty = sqlProperty + "zhifushenhe,";
                sqlValue = sqlValue + "'" + GetControl("zhifushenhe").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("fahuoshenhe"))
            {
                IsEmptyControl("fahuoshenhe");
    
                sqlProperty = sqlProperty + "fahuoshenhe,";
                sqlValue = sqlValue + "'" + GetControl("fahuoshenhe").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("shouhuoshenhe"))
            {
                IsEmptyControl("shouhuoshenhe");
    
                sqlProperty = sqlProperty + "shouhuoshenhe,";
                sqlValue = sqlValue + "'" + GetControl("shouhuoshenhe").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("zhifufangshiid"))
            {
                IsEmptyControl("zhifufangshiid");
    
                sqlProperty = sqlProperty + "zhifufangshiid,";
                sqlValue = sqlValue + "'" + GetControl("zhifufangshiid").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("xingming"))
            {
                IsEmptyControl("xingming");
    
                sqlProperty = sqlProperty + "xingming,";
                sqlValue = sqlValue + "'" + GetControl("xingming").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("dianhua"))
            {
                IsHandset("dianhua");
    
                sqlProperty = sqlProperty + "dianhua,";
                sqlValue = sqlValue + "'" + GetControl("dianhua").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("dizhi"))
            {
                IsEmptyControl("dizhi");
    
                sqlProperty = sqlProperty + "dizhi,";
                sqlValue = sqlValue + "'" + GetControl("dizhi").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("usersid"))
            {
                IsEmptyControl("usersid");
    
                sqlProperty = sqlProperty + "usersid,";
                sqlValue = sqlValue + "'" + GetControl("usersid").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("shijian"))
            {
                IsEmptyControl("shijian");
    
                sqlProperty = sqlProperty + "shijian,";
                sqlValue = sqlValue + "'" + GetControl("shijian").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("wuliu"))
            {
                IsEmptyControl("wuliu");
    
                sqlProperty = sqlProperty + "wuliu,";
                sqlValue = sqlValue + "'" + GetControl("wuliu").Text.ToString().Trim() + "',";
            }
﻿
            if (IsExistControl("beizhu"))
            {
                IsEmptyControl("beizhu");
    
                sqlProperty = sqlProperty + "beizhu,";
                sqlValue = sqlValue + "'" + GetControl("beizhu").Text.ToString().Trim() + "',";
            }



        sqlProperty = sqlProperty.Substring(0, sqlProperty.Length - 1);
        sqlValue = sqlValue.Substring(0, sqlValue.Length - 1);
        sql = sqlProperty + sqlValue + sqlTail;
        int result;
        result = new Class1().hsgexucute(sql);
        if (result == 1)
        {
            //购物车处理
            //1查询订单id
            sql = "select id from dingdan where bianhao='" + GetControl("bianhao").Text.ToString().Trim()+"'";
            string dingdanid = GetId(sql);
            if(dingdanid != "")
            {
                //2查询我的购物车，加入订单项，减库存

                //加入订单项
                for (int i = 0; i < dsresultgouwuche.Tables[0].Rows.Count; i++)
                {
                    string shangpinid = dsresultgouwuche.Tables[0].Rows[i]["shangpinid"].ToString().Trim();
                    string goumaishuliang = dsresultgouwuche.Tables[0].Rows[i]["goumaishuliang"].ToString().Trim();

                    string kucunshuliang = getFatherSecondColumnValue("shangpin", "kucunshuliang", shangpinid);
                    string kucunshuliangx = (int.Parse(kucunshuliang) - int.Parse(goumaishuliang) ).ToString();
                    string sqlshangpinupdt = "update shangpin set kucunshuliang='"+ kucunshuliangx +"' where id='"+ shangpinid +"'";
                    new Class1().hsgexucute(sqlshangpinupdt);//执行sql语句更新库存

                    string xiaoshoushuliang = getFatherSecondColumnValue("shangpin", "xiaoshoushuliang", shangpinid);
                    string xiaoshoushuliangx = (int.Parse(xiaoshoushuliang) + int.Parse(goumaishuliang) ).ToString();
                     sqlshangpinupdt = "update shangpin set xiaoshoushuliang='"+xiaoshoushuliangx+"' where id='"+ shangpinid +"'";
                    new Class1().hsgexucute(sqlshangpinupdt);//执行sql语句更新销量

                    if (result == 1)
                    {

                    }
                    else
                    {
                        Response.Write("<script>javascript:alert('系统错误，请检查数据库设置问题');history.back();</script>");
                        return;
                    }
                }
                //循环结束
                //2清空购物车
                string sql2="delete from gouwuche  where usersid='"+ usersid +"'";
                new Class1().hsgexucute(sql2);
                Response.Write("<script>javascript:alert('操作完成，继续操作！');history.back();</script>");
                return;


            }



        }

        Response.Write("<script>javascript:alert('系统错误，请检查数据库设置问题');history.back();</script>");
        return;
    }

    //选择父类信息
    private void addselect(string FatherTable, string FatherIdColumnName, string FatherSecondColumnName, string DropDownListControlId)
    {
        //控件不存在则返回
        if (this.FindControl(DropDownListControlId) == null)
        {
            //Response.Write("控件未找到!!!   " + DropDownListControlId);
            return;
        }

        string sql;
        sql = "select " + FatherIdColumnName + "," + FatherSecondColumnName + " from " + FatherTable + " order by id desc";
        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {

                //ListItem it0 = new ListItem();
                //it0.Value = "";
                //it0.Text = "全部";
                //((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it0);
                for (int i = 0; i < dsresult.Tables[0].Rows.Count; i++)
                {
                    ListItem it = new ListItem();
                    it.Value = dsresult.Tables[0].Rows[i][0].ToString().Trim();
                    it.Text = dsresult.Tables[0].Rows[i][1].ToString().Trim();
                    ((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it);
                }

            }
        }
    }

    //通过当前信息id作为父类id选择父类
    private void addselectbyId(string FatherTable, string FatherIdColumnName, string FatherSecondColumnName, string DropDownListControlId, string FatherIdColumnValue)
    {
        //控件不存在则返回
        if (this.FindControl(DropDownListControlId) == null)
        {
            //Response.Write("控件未找到!!!   " + DropDownListControlId);
            return;
        }

        string sql;
        sql = "select " + FatherIdColumnName + "," + FatherSecondColumnName + " from " + FatherTable + " where " + FatherIdColumnName + "='" + FatherIdColumnValue + "'" + " order by id desc";
        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {

                //ListItem it0 = new ListItem();
                //it0.Value = "";
                //it0.Text = "全部";
                //((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it0);
                for (int i = 0; i < dsresult.Tables[0].Rows.Count; i++)
                {

                    ListItem it = new ListItem();
                    it.Value = dsresult.Tables[0].Rows[i][0].ToString().Trim();
                    it.Text = dsresult.Tables[0].Rows[i][1].ToString().Trim();
                    ((DropDownList)this.FindControl(DropDownListControlId)).Items.Add(it);
                }

            }
        }
    }

    //获取父类信息
    private string getFatherSecondColumnValue(string FatherTable, string FatherSecondColumnName, string FatherIdColumnVaule)
    {
        string FatherSecondColumnValue = "";
        string sql;
        sql = "select " + FatherSecondColumnName + " from " + FatherTable + " where 1=1 and id='" + FatherIdColumnVaule + "' order by id desc";

        DataSet dsresult = new DataSet();
        dsresult = new Class1().hsggetdata(sql);
        if (dsresult != null)
        {
            if (dsresult.Tables[0].Rows.Count > 0)
            {
                FatherSecondColumnValue = dsresult.Tables[0].Rows[0][0].ToString().Trim();
            }
        }

        return FatherSecondColumnValue;
    }

    public void ischongfu(string sql)//是否重复判断
    {
        DataSet result = new DataSet();
        result = new Class1().hsggetdata(sql);
        if (result != null)
        {
            if (result.Tables[0].Rows.Count > 0)
            {
                Response.Write("<script>javascript:alert('提示,信息已存在,不要重复添加');history.back();</script>");
                Response.End();
            }
        }
    }

    public string GetId(string sql)//获取sql条件是唯一的id值
    {
        DataSet result = new DataSet();
        result = new Class1().hsggetdata(sql);
        if (result != null)
        {
            if (result.Tables[0].Rows.Count > 0)
            {
                return result.Tables[0].Rows[0][0].ToString().Trim();
            }
        }
        return "";
    }

    public void IsEmptyControl(string ControlId)//判断控件内容是否为空
    {
        if (!IsExistControl(ControlId) || new Class1().IsEmpty(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入不能为空,请重新输入!');history.back();</script>");
            Response.End();
        }
    }


    public void IsHandset(string ControlId)//判断控件内容是否手机号
    {
        IsEmptyControl(ControlId);
        if (!new Class1().IsHandset(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入格式不正确,请重新输入手机号格式!');history.back();</script>");
            Response.End();
        }
    }

    public void IsEmail(string ControlId)//判断控件内容是否邮箱
    {
        IsEmptyControl(ControlId);
        if (!new Class1().IsEmail(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入格式不正确,请重新输入邮箱格式!');history.back();</script>");
            Response.End();
        }
    }

    public void IsIDcard(string ControlId)//判断控件内容是否身份证号
    {
        IsEmptyControl(ControlId);
        if (!new Class1().IsIDcard(GetControl(ControlId).Text.ToString().Trim()))
        {
            Response.Write("<script>javascript:alert('输入格式不正确,请重新输入身份证格式!');history.back();</script>");
            Response.End();
        }
    }

    public bool IsExistControl(string DropDownListControlId)  //判断控件是否存在
    {
        if (this.FindControl(DropDownListControlId) != null)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    public ITextControl GetControl(string ControlId)  //获取ITextControl控件对象
    {
        if (this.FindControl(ControlId) != null)
        {
            return (ITextControl)this.FindControl(ControlId);
        }
        else
        {
            return null;
        }
    }

    public TextBox GetTextBoxControl(string TextBoxControlId)  //获取TextBox控件对象
    {
        if (this.FindControl(TextBoxControlId) != null)
        {
            return (TextBox)this.FindControl(TextBoxControlId);
        }
        else
        {
            return null;
        }
    }

    public DropDownList GetDropDownListControl(string DropDownListControlId)  //获取DropDownList控件对象
    {
        if (this.FindControl(DropDownListControlId) != null)
        {
            return (DropDownList)this.FindControl(DropDownListControlId);
        }
        else
        {
            return null;
        }
    }

}

